home *** CD-ROM | disk | FTP | other *** search
/ C++ für Kids / C++ for kids.iso / SETUP / US / CBUILDER / DATA.Z / MEMORY.H < prev    next >
C/C++ Source or Header  |  1997-02-13  |  14KB  |  508 lines

  1. /*  memory.h
  2.  
  3.     Memory manipulation functions
  4.  
  5. */
  6.  
  7. /*
  8.  *      C/C++ Run Time Library - Version 8.0
  9.  *
  10.  *      Copyright (c) 1991, 1997 by Borland International
  11.  *      All Rights Reserved.
  12.  *
  13.  */
  14. /* $Revision:   8.1  $ */
  15.  
  16. #if !defined(__USING_STD_NAMES__)
  17.  
  18. #include <mem.h>
  19.  
  20. #else   /* __USING_STD_NAMES__ */
  21.  
  22. #ifndef __STD_MEMORY
  23. #define __STD_MEMORY
  24.  
  25. /***************************************************************************
  26.  *
  27.  * memory - declarations for the Standard Library memory implementation
  28.  *
  29.  * $Id: memory,v 1.38 1995/09/14 02:50:59 smithey Exp $
  30.  *
  31.  ***************************************************************************
  32.  *
  33.  * Copyright (c) 1994
  34.  * Hewlett-Packard Company
  35.  *
  36.  * Permission to use, copy, modify, distribute and sell this software
  37.  * and its documentation for any purpose is hereby granted without fee,
  38.  * provided that the above copyright notice appear in all copies and
  39.  * that both that copyright notice and this permission notice appear
  40.  * in supporting documentation.  Hewlett-Packard Company makes no
  41.  * representations about the suitability of this software for any
  42.  * purpose.  It is provided "as is" without express or implied warranty.
  43.  *
  44.  *
  45.  ***************************************************************************
  46.  *
  47.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  48.  * ALL RIGHTS RESERVED
  49.  *
  50.  * The software and information contained herein are proprietary to, and
  51.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  52.  * intends to preserve as trade secrets such software and information.
  53.  * This software is furnished pursuant to a written license agreement and
  54.  * may be used, copied, transmitted, and stored only in accordance with
  55.  * the terms of such license and with the inclusion of the above copyright
  56.  * notice.  This software and information or any other copies thereof may
  57.  * not be provided or otherwise made available to any other person.
  58.  *
  59.  * Notwithstanding any other lease or license that may pertain to, or
  60.  * accompany the delivery of, this computer software and information, the
  61.  * rights of the Government regarding its use, reproduction and disclosure
  62.  * are as set forth in Section 52.227-19 of the FARS Computer
  63.  * Software-Restricted Rights clause.
  64.  *
  65.  * Use, duplication, or disclosure by the Government is subject to
  66.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  67.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  68.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  69.  * P.O. Box 2328, Corvallis, Oregon 97339.
  70.  *
  71.  * This computer software and information is distributed with "restricted
  72.  * rights."  Use, duplication or disclosure is subject to restrictions as
  73.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  74.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  75.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  76.  * then the "Alternate III" clause applies.
  77.  *
  78.  **************************************************************************/
  79.  
  80. #include <stdcomp.h>
  81.  
  82. #ifndef RWSTD_NO_NEW_HEADER
  83. #include <cstddef>
  84. #include <cstdlib>
  85. #include <new>
  86. #else
  87. #include <new.h>
  88. #include <stddef.h>
  89. #include <stdlib.h>
  90. #endif
  91.  
  92. #include <limits>
  93.  
  94. #ifdef RW_STD_IOSTREAM
  95. #include <iostream>
  96. #else
  97. #include <iostream.h>
  98. #endif
  99.  
  100. #include <iterator>
  101. #include <utility>
  102.  
  103. #ifdef RWSTD_MULTI_THREAD
  104. #include <stdmutex.h>
  105. #endif
  106.  
  107. #ifndef RWSTD_NO_NAMESPACE
  108. namespace std {
  109. #endif
  110.  
  111. #ifdef RWSTD_NO_NEW_DECL
  112. inline void* operator new (size_t, void* p) { return p; }
  113. #endif
  114.  
  115. //
  116. // Raw storage iterator.
  117. //
  118.  
  119. template <class OutputIterator, class T>
  120. class raw_storage_iterator : public output_iterator
  121. {
  122.   protected:
  123.     OutputIterator iter;
  124.   public:
  125.     explicit raw_storage_iterator (OutputIterator x) : iter(x) {}
  126.     raw_storage_iterator<OutputIterator, T>& operator* () { return *this; }
  127.     raw_storage_iterator<OutputIterator, T>& operator= (const T& element)
  128.     {
  129.         construct(iter, element); return *this;
  130.     }
  131.     raw_storage_iterator<OutputIterator, T>& operator++ ()
  132.     {
  133.         ++iter; return *this;
  134.     }
  135.     raw_storage_iterator<OutputIterator, T> operator++ (int)
  136.     {
  137.         raw_storage_iterator<OutputIterator, T> tmp = *this;
  138.         ++iter;
  139.         return tmp;
  140.     }
  141. };
  142.  
  143. //
  144. // Memory handling primitives.
  145. //
  146.  
  147. template <class T>
  148. inline T* allocate (int size, T*)
  149. {
  150. #ifndef RWSTD_NO_NAMESPACE
  151.     T* tmp = (T*)(std::operator new((unsigned int)(size * sizeof(T))));
  152. #else
  153.     T* tmp = (T*)(::operator new((unsigned int)(size * sizeof(T))));
  154. #endif
  155.     if (tmp == 0)
  156.     {
  157.         cerr << "out of memory" << endl;
  158.         exit(1);
  159.     }
  160.     return tmp;
  161. }
  162.  
  163. #ifndef RWSTD_NO_ARG_MATCH
  164. template <class T>
  165. inline T* allocate (long size, T*)
  166. {
  167. #ifndef RWSTD_NO_NAMESPACE
  168.     T* tmp = (T*)(std::operator new((unsigned long)(size * sizeof(T))));
  169. #else
  170.     T* tmp = (T*)(::operator new((unsigned long)(size * sizeof(T))));
  171. #endif
  172.     if (tmp == 0)
  173.     {
  174.         cerr << "out of memory" << endl;
  175.         exit(1);
  176.     }
  177.     return tmp;
  178. }
  179. #endif
  180.  
  181. template <class T>
  182. inline void deallocate (T* buffer)
  183. {
  184. #ifndef RWSTD_NO_NAMESPACE
  185.     std::operator delete(buffer);
  186. #else
  187.     ::operator delete(buffer);
  188. #endif
  189. }
  190.  
  191. template <class T1, class T2>
  192. inline void construct (T1* p, const T2& value)
  193. {
  194.     new (p) T1(value);
  195. }
  196.  
  197. #if defined(RWSTD_NO_DESTROY_NONBUILTIN)
  198. template <class T> struct __FS : public T
  199. {
  200.     //
  201.     // Calls destructor, but does not free the space.
  202.     //
  203.     void operator delete (void*) {;}
  204. };
  205. #endif
  206.  
  207. template <class T>
  208. inline void destroy (T* pointer)
  209. {
  210. #if defined(RWSTD_NO_DESTROY_NONBUILTIN)
  211.     delete (__FS<T>*) (pointer);
  212. #else
  213.     pointer->~T();
  214. #endif
  215. }
  216.  
  217. template <class Pointer>
  218. void destroy (Pointer first, Pointer last)
  219. {
  220.     while (first != last)
  221.     {
  222.         destroy(first);
  223.         ++first;
  224.     }
  225. }
  226.  
  227. #ifdef RWSTD_FAST_TEMP_BUF
  228.  
  229. #ifndef __stl_buffer_size
  230. #define __stl_buffer_size 16384  /* 16k */
  231. #endif
  232.  
  233. extern char __stl_temp_buffer[__stl_buffer_size];
  234.  
  235. #ifdef RWSTD_MULTI_THREAD
  236. extern RWSTDMutex __stl_temp_buffer_mutex;
  237. extern bool       __stl_temp_buffer_being_used;
  238. #endif
  239.  
  240. template <class T>
  241. pair<T*, int> get_temporary_buffer (int len, T*)
  242. {
  243.     while (len > __stl_buffer_size / sizeof(T))
  244.     {
  245. #ifndef RWSTD_NO_NAMESPACE
  246.         T* tmp = (T*)(std::operator new((unsigned int)len * sizeof(T)));
  247. #else
  248.         T* tmp = (T*)(   ::operator new((unsigned int)len * sizeof(T)));
  249. #endif
  250.         if (tmp)
  251.         {
  252.             pair<T*, int> result(tmp, len);
  253.             return result;
  254.         }
  255.         len = len / 2;
  256.     }
  257.  
  258. #ifdef RWSTD_MULTI_THREAD
  259.     RWSTDGuard guard(__stl_temp_buffer_mutex);
  260.  
  261.     if (__stl_temp_buffer_being_used)
  262.     {
  263. #ifndef RWSTD_NO_NAMESPACE
  264.         T* tmp = (T*)(std::operator new((unsigned int)len * sizeof(T)));
  265. #else
  266.         T* tmp = (T*)(   ::operator new((unsigned int)len * sizeof(T)));
  267. #endif
  268.         pair<T*,int> result(tmp, len);
  269.         return result;
  270.     }
  271.     else
  272.     {
  273.         __stl_temp_buffer_being_used = true;
  274.         pair<T*, int> result((T*) __stl_temp_buffer,
  275.                              (int) (__stl_buffer_size / sizeof(T)));
  276.         return result;
  277.     }
  278. #else
  279.     pair<T*, int> result((T*) __stl_temp_buffer,
  280.                          (int) (__stl_buffer_size / sizeof(T)));
  281.     return result;
  282. #endif /*RWSTD_MULTI_THREAD*/
  283. }
  284.  
  285. template <class T>
  286. inline void return_temporary_buffer (T* p)
  287. {
  288. #ifdef RWSTD_MULTI_THREAD
  289.     RWSTDGuard guard(__stl_temp_buffer_mutex);
  290.  
  291.     if ((char*)(p) != __stl_temp_buffer)
  292.         deallocate(p);
  293.     else
  294.         __stl_temp_buffer_being_used = false;
  295. #else
  296.     if ((char*)(p) != __stl_temp_buffer)
  297.         deallocate(p);
  298. #endif /*RWSTD_MULTI_THREAD*/
  299. }
  300.  
  301. #else
  302.  
  303. template <class T>
  304. pair<T*, int> get_temporary_buffer (int len, T*)
  305. {
  306. #ifndef RWSTD_NO_NAMESPACE
  307.     T* tmp = (T*)(std::operator new((unsigned int)len * sizeof(T)));
  308. #else
  309.     T* tmp = (T*)(   ::ope